app.controller("itemCarController", function ($scope, $controller, itemCatService, typeTemplateService) {
    //继承
    $controller("baseController", {$scope: $scope});

    $scope.grade = 1;
    $scope.setGrade = function (value) {
        $scope.grade = value;
    }

    $scope.typeId = JSON.parse('{"id":37,"text":"电视"}');
    //把当前这个对象绑定到js中,对这个动态的 $scope.grade判断
    $scope.findBypid = function (entity) {
        if ($scope.grade == 1) {
            $scope.entity_1 = null;
            $scope.entity_2 = null;
        }
        if ($scope.grade == 2) {
            $scope.entity_1 = entity;
            $scope.entity_2 = null;
        }
        if ($scope.grade == 3) {//如果为3级
            $scope.entity_2 = entity;
        }
        /**
         *  当前的entity修改为当前的grade级别
         *  那么就能拿到他的父id和名字
         */
        $scope.entity = entity;
        $scope.reloadList();//重新加载
    }

    $scope.search = function (pageNum, pageSize) {
        itemCatService.search(pageNum, pageSize, $scope.entity.id).success(
            function (response) {
                $scope.list = response.rows;
                $scope.paginationConf.totalItems = response.total;
            })
    }

    $scope.typeList = {data: []};
    $scope.findOptions = function () {
        typeTemplateService.typeTemplateOptionList().success(
            function (response) {
                $scope.typeList = {data: response};
            })
    }

    $scope.deletes = function () {
        itemCatService.deletes($scope.selectIds).success(
            function (response) {
                if (response.success) {
                    $scope.reloadList();//刷新列表
                } else {
                    alert(response.message);
                }
            })
    }

    $scope.findOne = function (id) {
        itemCatService.findOne(id).success(
            function (response) {
                $scope.entityOne = response;
                var TypeList = $scope.typeList.data;
                for (var x = 0; x < TypeList.length; x++) {
                    if (TypeList[x].id == response.typeId) {
                        $scope.entityOne.ids = TypeList[x];
                    }
                }
                /*  //  查询模板下拉回显
                  typeTemplateService.findOneOption(response.typeId).success(
                      function (response) {
                          console.log(response)
                          $scope.entityOne.ids=response;
                  })*/

            })
    }
    $scope.save = function () {
        itemCatService.save($scope.entity.id, $scope.entityOne).success(
            function (response) {
                if (response.success) {
                    $scope.reloadList();//刷新列表
                } else {
                    alert(response.message);
                }
            })
    }
})